<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\CategoryForum;
use app\common\model\shop\Goods;
use app\common\model\trends\Comment;
use app\common\model\publics\Shop;
use think\Config;

/**
 * 集市产品信息接口
 */
class Shops extends Api
{
	protected $noNeedLogin = ['goods', 'goods_detail', 'comment_list'];
	protected $noNeedRight = ['*'];
	
	/**
	 * 分类
     * 
	 * @ApiMethod (POST)
	 * @param int $cate_id 分类id
	 */
	public function cate()
	{
		$cate_id = $this->request->post('cate_id', 0);
		$where = [];
		if ($cate_id) {
			$where['pid'] = $cate_id;
		} else {
			$where['pid'] = 0;
		}
		$list = CategoryForum::where($where)->select();
		$this->success("发布成功", $list);
	}
	
	/**
	 * 商品发布评论
     * 
	 * @ApiMethod (POST)
	 * @param int $comment_id 评论id
	 * @param int $goods_id   商品id，必填
	 * @param int $content    内容，必填
	 */
	public function add_comment()
	{
		$user_id    = $this->auth->id;
		$comment_id = $this->request->post('comment_id', 0);
		$goods_id   = $this->request->post('goods_id');
		$content    = $this->request->post('content');
		
		if (!$content) {
			$this->success("内容不能为空", [], 2);
		}
		//判断文字敏感字
		$res = msg_check($content);
		if($res['errcode'] != 0){
			$this->success("评论含有敏感信息", [], 2);
		}
		
		$goods = Goods::get($goods_id);
		
		if (!$goods) {
			$this->success("商品不存在", [], 2);
		}
		
		if ($comment_id) {
			$comment = Comment::get($comment_id);
			if (!$comment) {
				$this->success("回复的评论不存在", [], 2);
			}
		} else {
			$comment = '';
		}
		
		$data = [
			'type'        => 2,
			'user_id'     => $user_id,
			'user2_id'    => $goods->user_id,
			'trends_id'   => $goods_id,
			//'circle_id' => $Trends->circle_id,
			'parent_id'   => $comment ? $comment->id : 0,
			'hf_user_id'  => $comment ? $comment->user_id : 0,
			'content'     => $content,
			'create_time' => time(),
		];
		
		$res = Comment::create($data);
		if ($res) {
			$goods->setInc('comment', 1);
			if ($comment_id) {
				$this->circulate($comment_id);
			}
			//发布系统消息
			send($user_id, $goods->user_id, 2, 0);
			$this->success("发布成功");
		} else {
			$this->error('出差了');
		}
	}
	
	/**
     * 评论列表
     * 
	 * @ApiMethod (POST)
	 * @param int $goods_id   商品id，必填
	 * @param int $comment_id 评论id
	 * @param int $page       当前页码
	 * @param int $count      每页数量
     */
	public function comment_list()
	{
		$user_id    = $this->auth->id;
		$goods_id   = $this->request->post('goods_id', 0);
		$comment_id = $this->request->post('comment_id', 0);
		$page       = $this->request->post('page', 1);
		$count      = $this->request->post('count', 10);
		
		$goods = Goods::get($goods_id);
		
		if (!$goods) {
			$this->success("商品不存在", [], 2);
		}
		
		$where              = [];
		$where['type']      = 2;
		$where['trends_id'] = $goods_id;
		$circle_id          = Config::get("site2.ggqz");
		if ($comment_id) {
			$where['parent_id'] = $comment_id;
		} else {
			$where['parent_id'] = 0;
		}
		
		$list = Comment::where($where)
		               ->order("create_time desc")
		               ->paginate($count, false, ["page" => $page]);
		
		foreach ($list as $row) {
			$row->append(['userinfo', 'comment']);
			$row->userinfo = $row->getuser($row, $user_id, $circle_id);
			//$list = '';//Comment::where(['id'=>$row->trends_id])->order("create_time desc")->limit(3)->select();
			$list2 = Comment::where(['parent_id' => $row->id])->order("create_time desc")->limit(3)->select();

			//新增
			foreach ($list2 as $row2){
				$row2->append(['huifu3']);
				$huifu3 = Comment::where(['parent_id' => $row2->id])->order("create_time asc")->find();
				if($huifu3){
					$row2->huifu3 = $huifu3;
				}else{
					$row2->huifu3 = [];
				}
			}
			$row->comment = $list2;
		}
		$this->success("成功", $list);
	}
	
	//上级评论循环增加点赞数量
	private function circulate($comment_id)
	{
		$comment = Comment::get($comment_id);
		if ($comment) {
			$comment->setInc('num', 1);
			if ($comment->parent_id) {
				$this->circulate($comment->parent_id);
			}
		}
		return true;
	}
	
	/**
	 * 添加商品
     * 
	 * @ApiMethod (POST)
	 * @param 表单数组
	 * @return void
	 */
	public function add_goods()
	{
		$user_id  = $this->auth->id;
		$param    = $this->request->post();
		$goods_id = $this->request->post('goods_id', 0);
		
		$this->rule = [
			
			"circle_id|圈子ID"    => ["require"],
			"title|标题"          => ["require"],
			"price|金额"          => ["require"],
			"detail|描述"         => ["require"],
			"cate_id|一级分类ID"  => ["require"],
			"cate2_id|二级分类ID" => ["require"],
			"is_contact|联系方式" => ["require"],
			"contact|联系号码"    => ["require"],
			"images|图片"         => ["require"],
		
		];
		
		$this->checks($param);
		
		$param["user_id"]     = $user_id;
		$param["create_time"] = time();
		unset($param["goods_id"]);
		
		if ($goods_id) {
			$goods = Goods::get($goods_id);
			if (!$goods) {
				$this->success('商品不存在', [], 2);
			}
			$res = $goods->save($param);
			
		} else {
			$res = Goods::create($param, true);
		}
		
		if ($res) {
			$this->success('发布成功');
		} else {
			$this->error("出差了");
		}
		
	}
	
	/**
	 * 商品上下架 删除
     * 
	 * @ApiMethod (POST)
	 * @param int $goods_id   商品id，必填
	 * @param int $type       类型，必填 （1 上架，2 下架，3 删除）
	 */
	public function up_goods()
	{
		$user_id  = $this->auth->id;
		$type     = $this->request->post('type', 0);
		$goods_id = $this->request->post('goods_id', 0);
		
		if (!$goods_id || !$type) {
			$this->success('参数错误', [], 2);
		}
		
		$goods = Goods::where(['user_id' => $user_id, 'id' => $goods_id])->find();
		if (!$goods) {
			$this->success('商品不存在', [], 2);
		}
		if ($type == 1) {
			$res = $goods->save(['is_show' => $type, 'updatetime' => time()]);
		} else if ($type == 2) {
			$res = $goods->save(['is_show' => $type, 'updatetime' => time()]);
		} else if ($type == 3) {
			$res = $goods->delete();
		}
		
		if ($res) {
			$this->success('成功');
		} else {
			$this->error("出差了");
		}
	}
	
	/**
	 * 商品列表
     * 
	 * @ApiMethod (POST)
	 * @param int $cate_id    分类id
	 * @param int $title      商品标题
	 * @param int $page       当前页码
	 * @param int $count      每页数量
	 * @param int $circle_id  圈子id
	 * @return void
	 */
	public function goods()
	{
		$user_id   = $this->auth->id;
		$cate_id   = $this->request->post('cate_id', 0);
		$title     = $this->request->post('title', '');
		$page      = $this->request->post('page', 1);
		$count     = $this->request->post('count', 10);
		$circle_id = $this->request->post('circle_id');
		
		$where            = [];
		$where['is_show'] = 1;
		$where['status']  = 1;
		
		if ($cate_id) {
			$where['cate_id'] = $cate_id;
		}
		
		if ($circle_id) {
			$where["circle_id"] = $circle_id;
		}
		
		if ($title) {
			$where['title'] = ["like", "%$title%"];
		}
		
		$list = Goods::where($where)
		             ->order("id desc")
		             ->paginate($count, false, ["page" => $page]);
		
		$this->success('成功', $list);
	}
	
	/**
	 * 我的发布
     * 
	 * @ApiMethod (POST)
	 * @param int $circle_id  圈子id
	 * @param int $page       当前页码
	 * @param int $count      每页数量
	 * @return void
	 */
	public function my_goods()
	{
		$user_id   = $this->auth->id;
		$circle_id = $this->request->post('circle_id');
		$page      = $this->request->post('page', 1);
		$count     = $this->request->post('count', 10);
		
		$where            = [];
		$where['user_id'] = $user_id;
		
		if ($circle_id) {
			$where["circle_id"] = $circle_id;
		}
		
		$list = Goods::where($where)
		             ->order("id desc")
		             ->paginate($count, false, ["page" => $page]);
		
		$this->success('成功', $list);
	}
	
	/**
	 * 商品详情
     * 
	 * @ApiMethod (POST)
	 * @param int $goods_id  商品id
	 */
	public function goods_detail()
	{
		$user_id  = $this->auth->id;
		$goods_id = $this->request->post('goods_id', 0);
		
		$where           = [];
		$where['status'] = 1;
		$where['id']     = $goods_id;
		
		$detail = Goods::where($where)->find();
		
		if (!$detail) {
			$this->success('商品不存在或已下架', [], 2);
		}
		$this->success('成功', $detail);
	}
	
	/**
	  * 商家入驻详情
	  */
	public function shop_details()
	{
		$user_id  = $this->auth->id;
		//$user_id  = 136574;
		$where           = [];
		$where['user_id']     = $user_id;
		$detail = Shop::where($where)->find();
		if (!$detail) {
			$this->error('商家入驻信息不存在');
		}
		$this->success('商家入驻信息', $detail);
	}
}
